package cn.pink.gamesrv.human.module.item;

import cn.hutool.json.JSONObject;
import cn.pink.common.config.ConfItem;
import cn.pink.common.entity.game.item.EntityHumanItem;
import cn.pink.common.entity.game.item.EntityHumanItemData;
import cn.pink.common.proto.Msg;
import cn.pink.core.Port;
import cn.pink.gamesrv.human.HumanObject;
import cn.pink.gamesrv.human.module.IMod;
import cn.pink.logicsrv.RPCProxy;
import com.google.protobuf.GeneratedMessageV3;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author: pink
 * @Date: 2022/7/25 11:28
 */
public class ModItem implements IMod {
    HumanObject humanObj;

    EntityHumanItem humanItemDB;

    public ModItem(HumanObject humanObj) {
        this.humanObj = humanObj;
    }

    public void load(Map<String, Object> data) {
        if(data == null) {
            humanItemDB = (EntityHumanItem) humanObj.createEntityProxy(EntityHumanItem.class).getEntity();

            Map<Integer, EntityHumanItemData> itemDataMap = new HashMap<>();
            for(int i = 0; i < 10; i++) {
                int mid = 10000 + i;

                EntityHumanItemData humanItemDataDB = (EntityHumanItemData) humanObj.createEntityProxy(EntityHumanItemData.class, mid).getEntity();
                humanItemDataDB.setSn(11111);
                humanItemDataDB.setNum(100);

                itemDataMap.put(mid, humanItemDataDB);
            }

            humanItemDB.setItemMap(itemDataMap);
            humanItemDB.setTestInt(521);

            Map<Integer, String> testMap = new HashMap<>();
            testMap.put(5555, "abv");
            humanItemDB.setTestMap(testMap);
        }
        else {
            humanItemDB = (EntityHumanItem) humanObj.createAndInitEntityProxy(EntityHumanItem.class, data).getEntity();

            humanItemDB.setItemMap(new HashMap<>());

            JSONObject itemMap = (JSONObject) data.get("itemMap");
            itemMap.forEach(item -> {
                int mid = Integer.parseInt(item.getKey());
                JSONObject val = (JSONObject) item.getValue();

                EntityHumanItemData d = (EntityHumanItemData) humanObj.createAndInitEntityProxy(EntityHumanItemData.class, mid, val).getEntity();
                humanItemDB.getItemMap().put(mid, d);
            });

            humanItemDB.setTestInt(99999);

            humanItemDB.getTestMap().put(77888, "lcg_my");
            humanItemDB.setTestMap(humanItemDB.getTestMap());

            EntityHumanItemData d =  humanItemDB.getItemMap().get(10001);
            d.setSn(787878);
            d.setNum(2231111);
            d.setGrantTime(Port.getTime());
        }
    }

    @Override
    public void afterReadData() {
        System.out.println("mod item afterReadData ---- " + humanObj.getId());
    }

    @Override
    public void loginFinish() {
        System.out.println("mod item loginFinish ---- " + humanObj.getId());
    }

    @Override
    public GeneratedMessageV3 build2ClientData() {
        Msg.DItemSPO.Builder spo = Msg.DItemSPO.newBuilder();

        humanItemDB.getItemMap().forEach((k, v) -> {
            Msg.DStrInt.Builder itemData = Msg.DStrInt.newBuilder();
            itemData.setKey(k + "");
            itemData.setValue(v.getNum());

            spo.addItemData(itemData);
        });

        humanItemDB.getTestMap().forEach((k, v) -> {
            Msg.DStrStr.Builder map = Msg.DStrStr.newBuilder();
            map.setKey(k + "");
            map.setValue(v);

            spo.addTestMap(map);
        });

        return spo.build();
    }

    public void test(String test) {
        System.out.println("test ---------- " + humanObj.getId());

        ConfItem confItem = ConfItem.get(50040006);
        System.out.println(confItem);

        RPCProxy.LogicServiceProxy proxy = RPCProxy.LogicServiceProxy.newInstance();
        proxy.defaultHandler(test);

//        RPCProxy.CenterServiceProxy proxy = RPCProxy.CenterServiceProxy.newInstance();
//        proxy.defaultHandler("ssssssssssdddddddddd");
    }
}
